class Solution {
    boolean[][] rowCheck;//行
    boolean[][] colCheck;//列
    boolean[][][] grip;//小方格

    public void solveSudoku(char[][] board) {
        rowCheck = new boolean[9][10];
        colCheck = new boolean[9][10];
        grip = new boolean[3][3][10];
        for (int i = 0; i < 9; i++) {
            for (int j = 0; j < 9; j++) {
                if (board[i][j] != '.') {
                    int num = board[i][j] - '0';
                    rowCheck[i][num] = colCheck[j][num] = grip[i / 3][j / 3][num] = true;
                }
            }
        }
        dfs(board);
    }

    public boolean dfs(char[][] board) {

        for (int row = 0; row < 9; row++) {
            for (int col = 0; col < 9; col++) {
                if (board[row][col] == '.') {
                    //填数
                    for (int val = 1; val <= 9; val++) {
                        //剪枝
                        if (!rowCheck[row][val] && !colCheck[col][val] && !grip[row / 3][col / 3][val]) {
                            board[row][col] = (char)('0' + val);//要强转的数字要加小括号
                            rowCheck[row][val] = colCheck[col][val] = grip[row / 3][col / 3][val] = true;
                            boolean check = dfs(board);
                            if(!check) {
                                //不满足条件 --> 回溯
                                board[row][col] ='.';
                                rowCheck[row][val] = colCheck[col][val] = grip[row / 3][col / 3][val] = false;
                            }else {
                                return true;
                            }
                        }
                    }
                    //1~9均不可填充
                    return false;
                }
            }
        }
        //此时表格已全部填满
        return true;
    }
}